using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE80;
using EnvDTE;
using System.Resources;
using System.Globalization;
using Microsoft.VisualStudio.CommandBars;
using System.Reflection;

namespace Tortuga.Addin {
	public class ExtensibilityUtilities {
		private AddIn _addinInstance = null;
		private DTE2 _applicationObject = null;
		private CultureInfo _applicationCulture = null;
		private ResourceManager _applicationResourceManager = null;
		private Commands2 _applicationCommands = null;
		private object[] _contextGuids = new object[] { };

		public ExtensibilityUtilities(AddIn addin, DTE2 application) {
			_addinInstance = addin;
			_applicationObject = application;
		}

		#region Properties

		public AddIn AddInInstance {
			get { return _addinInstance; }
			set { _addinInstance = value; }
		}

		public DTE2 ApplicationObject {
			get { return _applicationObject; }
			set { _applicationObject = value; }
		}


		#region Limited

		public CultureInfo ApplicationCulture {
			get {
				if (_applicationCulture == null) _applicationCulture = new CultureInfo(ApplicationObject.LocaleID);
				return _applicationCulture;
			}
			protected set { _applicationCulture = value; }
		}

		public ResourceManager ApplicationResourceManager {
			get {
				if (_applicationResourceManager == null) {
					_applicationResourceManager = new ResourceManager(
						String.Format("{0}.CommandBar", AddInInstance.ProgID),
						Assembly.GetExecutingAssembly()); // I'm not really sure if this will work if moved out of this assembly
				}
				return _applicationResourceManager;
			}
			protected set { _applicationResourceManager = value; }

		}

		public Commands2 ApplicationCommands {
			get {
				return _applicationObject.Commands as Commands2;
				return _applicationCommands;
			}
			protected set { _applicationCommands = value; }
		}

		protected object[] ContextGuids {
			get { return _contextGuids; }
			set { _contextGuids = value; }
		}
		#endregion

		#endregion


		public string FindToolsMenuName() {
			string toolsMenuName = "";
			try {
				ResourceManager resourceManager = new ResourceManager(
					String.Format("{0}.CommandBar", AddInInstance.ProgID), 
					Assembly.GetExecutingAssembly());

				CultureInfo cultureInfo = new System.Globalization.CultureInfo(_applicationObject.LocaleID);
				toolsMenuName = resourceManager.GetString(
					String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"));
			} catch {
				toolsMenuName = "Tools";
			}
			return toolsMenuName;
		}

		#region Command Bars
		public CommandBar FindCommandBar(string commandBarName) {
			return ((CommandBars)ApplicationObject.CommandBars)[commandBarName] as CommandBar;
		}

		public CommandBarControl FindCommandBarControl(CommandBar commandBar, string controlName) {
			return commandBar.Controls[controlName];
		}

		public T FindCommandBarControl<T>(CommandBar commandBar, string controlName) where T : CommandBarControl {
			return (T)FindCommandBarControl(commandBar, controlName);
		}

		#endregion

		#region Commands

		public Command CreateCommand(string commandName, string buttonText) {
			return CreateCommand(commandName, buttonText, "", Missing.Value);
		}

		public Command CreateCommand(string commandName, string buttonText, string tooltip, object bitmapID) {
			return CreateCommand(commandName, buttonText, tooltip, bitmapID, null);
		}


		public Command CreateCommand(string commandName, string buttonText, string tooltip, 
			object bitmapID, _dispCommandBarControlEvents_ClickEventHandler onClick) {
			Command command = ApplicationCommands.AddNamedCommand2(
				AddInInstance, commandName, buttonText, tooltip,
				true, bitmapID, ref _contextGuids,
				(int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
				(int)vsCommandStyle.vsCommandStylePictAndText,
				vsCommandControlType.vsCommandControlTypeButton);
			if(onClick != null)
				AddCommandClickHandler(command as CommandBarControl, onClick);
			
			return command;
		}


		public object AddToPopup(CommandBarPopup popup, Command command) {			
			return command.AddControl(popup, 1);
		}

		public object AddToPopup(CommandBarPopup popup, Command command, int position) {
			return command.AddControl(popup, position);			
		}

		#endregion

		#region Menus

		public CommandBarPopup CreatePopupMenu(string name, CommandBar parentMenu) {
			CommandBarPopup popup = parentMenu.Controls.Add(MsoControlType.msoControlPopup,
				Missing.Value, Missing.Value, Missing.Value, true) as CommandBarPopup;
			popup.Caption = name;
			return popup;
		}

		public CommandBarPopup CreatePopupMenu(string name, CommandBarPopup parentMenu) {
			CommandBarPopup popup = parentMenu.Controls.Add(MsoControlType.msoControlPopup,
				Missing.Value, Missing.Value, Missing.Value, true) as CommandBarPopup;
			popup.Caption = name;
			return popup;
		}

		public void AddCommandClickHandler(CommandBarControl command, _dispCommandBarControlEvents_ClickEventHandler onClick) {
			if (command != null) {
				CommandBarEvents handler = ApplicationObject.DTE.Events.get_CommandBarEvents(command) as CommandBarEvents;
				handler.Click += new _dispCommandBarControlEvents_ClickEventHandler(onClick);
			}
		}
		#endregion


	}
}
